<template>
  <div class="mod-study__project">
    <el-form :inline="true" :model="state.dataForm" @keyup.enter="state.getDataList()">
      <el-form-item label="模糊搜索">
        <el-input v-model="state.dataForm.keywords" placeholder="请输入" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="state.getDataList()">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button v-if="state.hasPermission('study:event:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="state.dataListLoading" :data="state.dataList" row-key="id" border style="width: 100%">
      <el-table-column prop="name" label="研学项目名称" header-align="center"></el-table-column>
      <el-table-column prop="studyType" label="研学类型" header-align="center">
        <template v-slot="scope">
          <span>{{ getStudyTypeName(scope.row.studyType) }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="statusStr" label="项目状态" header-align="center"></el-table-column>
      <el-table-column label="操作" fixed="right" header-align="center" align="center">
        <template v-slot="scope">
          <el-button v-if="state.hasPermission('study:project:status') && scope.row.status" type="primary" link @click="updateStatusHandle(scope.row)">停用</el-button>
          <el-button v-if="state.hasPermission('study:project:status') && !scope.row.status" type="primary" link @click="updateStatusHandle(scope.row)">启用</el-button>
          <el-button v-if="state.hasPermission('study:project:update') && !scope.row.status" type="primary" link @click="addOrUpdateHandle(scope.row.id)">编辑</el-button>
          <el-button v-if="state.hasPermission('study:project:delete')" type="primary" link @click="state.deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref, toRefs } from "vue";
import useView from "@/hooks/useView";
import baseService from "@/service/baseService";
import { ElMessage } from "element-plus";
import { studyTypeList } from "@/constants/config";
import { useRouter } from "vue-router";

const router = useRouter();
const view = reactive({
  getDataListURL: "/study/project/page",
  getDataListIsPage: true,
  deleteURL: "/study/project",
  deleteIsBatch: true,
  dataForm: {
    keywords: "",
  }
});

const state = reactive({ ...useView(view), ...toRefs(view) });

const addOrUpdateHandle = (id?: number) => {
  router.push({
    path: '/study/project/edit',
    query: {
      id,
    },
  });
};

const getStudyTypeName = (studyType: string) => {
  return studyTypeList.find((d) => d.value === studyType)?.label;
};

const updateStatusHandle = (row: any) => {
  baseService.post('/study/project/updateStatus', { id: row.id, status:  !row.status}).then((res) => {
      ElMessage.success({
        message: "成功",
        duration: 500,
        onClose: () => {
          state.getDataList();
        }
      });
    });
}
</script>
